import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:shop/http/HttpUtil.dart';
import 'package:shop/route/my_route.dart';
import 'package:shop/shop/life/CartOrder.dart';
import 'package:shop/shop/life/DizhiGuanli.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'LjgmOrder.dart';
class AddDizhi extends StatefulWidget {
  String gid;//商品ID
  int type;//下订单页面跳转 1：立即购买页面  2：购物车结算页面
  String names;//规格
  String params;//规格下标
  double money;//选定规格金额
  int num;//数量
  double allMoney;//总金额
  String orRemark;//订单备注
  AddDizhi({Key key, this.title, this.gid, this.type, this.names, this.params, this.money, this.num,this.allMoney,this.orRemark}) : super(key: key);

  final String title;

  @override
  _AddDizhi createState() => _AddDizhi(gid,type,names,params,money,num,allMoney,orRemark);
}

class _AddDizhi extends State<AddDizhi> {

  String gid;
  int type;
  String names;//规格
  String params;//规格下标
  double money;//选定规格金额
  int num;//数量
  double allMoney;//总金额
  String orRemark;//订单备注

  _AddDizhi(gid,type,names,params,money,num,allMoney,orRemark){
    this.type=type;
    this.gid=gid;
    this.names = names;
    this.params = params;
    this.money = money;
    this.num = num;
    this.allMoney = allMoney;
    this.orRemark = orRemark;
  }

  TextEditingController _unameController = new TextEditingController(); // 用户用户名从这里获取
  TextEditingController _telController = new TextEditingController();
  TextEditingController _dizhiController = new TextEditingController();
  bool isDefault=true;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text("添加收货地址"),
      ),
      body: Container(
        child: Column(
          // mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Container(
              child: TextFormField(
                obscureText: false,
                controller:_unameController ,
                decoration: InputDecoration(
                  labelText: "收件人姓名",
                ),
                onSaved: (str) {
                  _unameController.text = str;//str为文本的内容值
                },
              ),
            ),
            Container(
              child: TextFormField(
                controller: _telController,
                obscureText: false,
                decoration: InputDecoration(
                  labelText: "收件人电话",
                ),
                onSaved: (str) {
                  _telController.text = str;//str为文本的内容值
                },
              ),
            ),
            Container(
              child: TextFormField(
                controller: _dizhiController,
                obscureText: false,
                decoration: InputDecoration(
                  labelText: "详细收货地址",
                ),
                onSaved: (str) {
                  _dizhiController.text = str;//str为文本的内容值
                },
              ),
            ),
            Container(
              child: Row(
                children: [
                  Checkbox(value: isDefault, onChanged: (val){
                    isDefault=val;
                    setState(() {

                    });
                    },),
                  Container(child: Text("默认收货地址"),margin: EdgeInsets.only(right: 20),),
                  RaisedButton(
                    color: Colors.blue,
                    shape: RoundedRectangleBorder(
                    ),
                    onPressed: () async {
                      // post提示用户名或密码为空，get提交可以登录
                      SharedPreferences prefs = await SharedPreferences.getInstance();
                      String userId = prefs.getString("userId");
                      int adStatus = 0;
                      if(isDefault){
                        adStatus = 1;
                      }
                      var x = await HttpUtil().get("/app/address/addAddress?userId="+userId+"&addressDetail="+_dizhiController.text.trim()+"&adPhone="+_telController.text.trim()+"&adName="+_unameController.text.trim()+"&adStatus="+adStatus.toString());
                      // Response response = await Dio().get("http://xllm.szghrj.com:8082/app/address/addAddress",
                      //     queryParameters:{
                      //       "userId":userId,
                      //       "addressDetail":_dizhiController.text.trim(),
                      //       "adPhone":_telController.text.trim(),
                      //       "adName":_unameController.text.trim(),
                      //       "adStatus":adStatus
                      // });
                      // var x = json.decode(response.toString());
                      var dizhiId=x['msg'];
                      if(x['code']==200){
                        // Navigator.push() 带返回
                        // Navigator.pushReplacement() 不带返回
                        if(type==1){
                          MyRoute.push(LjgmOrder(adId:x['msg'],gid:gid,num: num,names: names,params: params,money: money,allMoney: allMoney,orRemark: orRemark,));//跳立即购买页面
                          // MyRoute.push(LjgmOrder(adId:x['msg'],gid:gid));//跳立即购买界面
                        }else if(type==2){
                          Map map=new Map();
                          map['adName']=_unameController.text.trim();
                          map['adPhone']=_telController.text.trim();
                          map['addressDetail']=_dizhiController.text.trim();
                          MyRoute.pop(map);//跳购物车结算界面
                        }else{
                          // MyRoute.replace(DizhiGuanli());
                          Map value={'id':dizhiId,'addressDetail':_dizhiController.text.trim(),'adPhone':_telController.text.trim(),'adName':_unameController.text.trim(),'adStatus':adStatus};
                          MyRoute.pop(value);
                        }
                      }else{
                        Fluttertoast.showToast(
                            msg: x['msg'],
                            toastLength: Toast.LENGTH_SHORT,
                            gravity: ToastGravity.BOTTOM,
                            timeInSecForIosWeb: 1,
                            backgroundColor: Colors.black45,
                            textColor: Colors.white,
                            fontSize: 16.0
                        );
                      }
                    },
                    child: Text("确定",),
                  ),
                ],
              )
            ),
          ],
        ),
      ),
    );
  }
}